package com.iwinding.ladygg.ui.act;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.iwinding.ladygg.event.AddFriendEvent;
import com.iwinding.ladygg.event.EventType;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.SearchFriendCallback;
import com.iwinding.ladygg.net.request.SearchFriendReq;
import com.iwinding.ladygg.net.response.UserRp;
import com.iwinding.ladygg.ui.adapter.AddContactAdapter;
import com.iwinding.ladygg.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import main.java.com.lwz.widget.searchbar.SearchBarView;

/**
 * Created by hezhiyong on 2017/4/6.
 */

public class SearchActivity extends BaseActivity {

    private SearchBarView mSearch;

    private ListView mListView;
    private AddContactAdapter adpater;

    private List<UserRp> userRps = new ArrayList<UserRp>();
    private String queryContent = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEvent(AddFriendEvent event) {
        if (event.type == EventType.ADD_FRIEND) {
            finish();
        }
    }

    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_search_friends;
    }

    void initView() {
        setTitleResId(R.string.icane_search_friends);

        mSearch = (SearchBarView) findViewById(R.id.search);
        mListView = (ListView) findViewById(R.id.pullListview);

        adpater = new AddContactAdapter(this, userRps);
        mListView.setAdapter(adpater);

        mSearch.setHintText(getString(R.string.icane_contacts_search_hint));
        mSearch.setOnSearchBarStateChnagedListener(
                new SearchBarView.SimpleSearchBarStateChangedListener() {

                    @Override
                    public boolean onSearchEvent(EditText searchEdit) {
                        queryContent = searchEdit.getEditableText().toString();
                        requestNet();
                        return super.onSearchEvent(searchEdit);
                    }

                    @Override
                    public void onPrepareSearch(EditText searchEidt) {
                    }

                    @Override
                    public void onCancel(EditText searchEidt) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start,
                                              int before, int count) {
                        queryContent = s.toString();
                        if (!TextUtils.isEmpty(queryContent)) {
                        }
//                        onPullDownToRefresh(mListView);
                    }

                });
    }

    void requestNet() {
        SearchFriendReq req = new SearchFriendReq();
        req.uname = queryContent;
        RequestManager.postSearchFriend(mContext,req,new SearchFriendCallback() {
            @Override
            public void onResult(UserRp userRp) {
                if (userRp != null) {
                        userRps.clear();
                        userRps.add(userRp);
                        adpater.notifyDataSetChanged();
                }
            }

            @Override
            public void onFail(String errorMsg) {
                ToastUtil.openBaseToast(SearchActivity.this, errorMsg, Toast.LENGTH_SHORT);
            }


        });
    }

}
